package bean;

import java.lang.reflect.Field;
/**
 * @author Ajie
 * @date 2023/2/22
 * @function
 */
public class BeanUtils {
    public static void copyFields(Object source, Object target) {
        Class<?> sourceClass = source.getClass();
        Class<?> targetClass = target.getClass();

        Field[] fields = sourceClass.getDeclaredFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            Class<?> fieldType = field.getType();

            try {
                Field targetField = targetClass.getDeclaredField(fieldName);
                if (targetField.getType().equals(fieldType)) {
                    field.setAccessible(true);
                    targetField.setAccessible(true);
                    Object value = field.get(source);
                    targetField.set(target, value);
                }
            } catch (NoSuchFieldException | IllegalAccessException e) {
//                e.printStackTrace();
            }
        }
    }


}
